﻿using System.Linq;
using System.Web.Mvc;
using web.core.ViewModels;
using web.core.Models;
using web.component;
using System.Web;

namespace MvcMusicStore.Controllers
{
    public class shoppingCartController : Controller
    {
       
        //
        // GET: /ShoppingCart/

        public ActionResult cart()
        {
            return PartialView("../shop/cart");
        }

       
        [ChildActionOnly]
        public ActionResult Carts(string type)
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

            // Set up our ViewModel
            var viewModel = new ShoppingCartViewModels
            {
                CartItems = cart.GetListCartItemByCartID(),
                CartTotal = cart.GetTotalValueCartItem(),
                CartCount = cart.GetCountCartItem()
            };

            if (!string.IsNullOrEmpty(type))
            {
                if (int.Parse(type) == (int)web.component.global_const.E_CART_TYPE.full_cart)
                {
                    return PartialView("../Shared/cart/carts", viewModel);
                }
                else if (int.Parse(type) == (int)web.component.global_const.E_CART_TYPE.mini_cart)
                {
                    return PartialView("../Shared/cart/mini_cart", viewModel);
                }
                else if (int.Parse(type) == (int)web.component.global_const.E_CART_TYPE.tiny_cart)
                {
                    return PartialView("../Shared/cart/tiny_cart", viewModel);
                }
                else {
                    return PartialView("../Shared/cart/carts", viewModel);
                }
            }
            else {
                return PartialView("../Shared/cart/carts", viewModel);
            }
            
        }
               
        public ActionResult AddToCart(int id)
        {
            //HttpCookie cookie = HttpContext.Request.Cookies["invoice"];

            //if (cookie != null)
            //{
            //    return RedirectToAction("PaymentOrder", "Checkout");
            //}
            //else
            //{
                // get Product
                var product = productModels.GetProductByID(id);

                // Add it to the shopping cart
                var cart = ShoppingCart.GetCart(this.HttpContext);

                cart.AddToCart(product);

                // Go back to the main store page for more shopping
                return RedirectToAction("Cart");
            //}
        }

        public ActionResult EmptyCart2()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);
            var list = cart.GetListCartItemByCartID();
            ShoppingCart shopcart = new ShoppingCart();
            foreach (var item in list)
            {
                shopcart.DeleteCartItem(item);
            }
            //shopcart.EmptyCart(cart);

            return Json("1");
        }

        public ActionResult EmptyCart()
        {
            ShoppingCart shopcart = new ShoppingCart();
            shopcart.EmptyCart();
            return Json("1");
        }

        public ActionResult AddMultiToCart(string id)
        {
            // Add it to the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext);
            var arr = id.Split(',');
            // get Product
            var product = productModels.GetProductByID(int.Parse(arr[0]));

            if (arr[1] != null && arr[1] != string.Empty)
            {
                int no = int.Parse(arr[1]);
                if (no > 0)
                {
                    for (int i = 0; i < no; i++)
                    {
                        cart.AddToCart(product);
                    }

                }
            }
            double CartTotal = cart.GetTotalValueCartItem();
            int CartCount = cart.GetCountCartItem();
            long totalCountCard = 0;
            long number = 0;
            var list = cart.GetListCartItemByCartID();
            foreach (var ite in list)
            {
                number = ite.count.Value;
                totalCountCard += number;
            }
            var results = totalCountCard + "|" + CartTotal + "|" + CartCount;
            return Json(results);
        }

        public ActionResult AddProductToCart(string id)
        {

            // Add it to the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext);
            // get Product
            var product = productModels.GetProductByID(int.Parse(id));

            cart.AddToCart(product);

            double CartTotal = cart.GetTotalValueCartItem();
            int CartCount = cart.GetCountCartItem();
            var list = cart.GetListCartItemByCartID();
            var list_cart_view = new System.Collections.Generic.List<CartViewModels>();
            foreach (var ite in list)
            {
                var _product = web.core.Models.productModels.GetProductViewByID(ite.product_id ?? 0, Request.Url.PathAndQuery);
                var cart_view =new web.core.ViewModels.CartViewModels();
                cart_view.record_id = ite.record_id;
                cart_view.product_id = ite.product_id;
                cart_view.product_name = _product.product_name;
                cart_view.product_alias = _product.product_alias;
                cart_view.price = _product.price;
                cart_view.quantity = _product.quantity;
                cart_view.images = _product.images;
                cart_view.count = ite.count;
                list_cart_view.Add(cart_view);
            }

            var results = web.bussines.my_extensions.ToJSON(list_cart_view) + "|" + CartTotal + "|" + CartCount;
            return Json(results);
        }

        [HttpPost]
        public ActionResult RemoveProductFromCart(string id)
        {
            // Remove the item from the cart
            var cart = ShoppingCart.GetCart(this.HttpContext);

            var cartItem = ShoppingCart.GetCartbyRecordID(int.Parse(id));
            // Get product
            var product = productModels.GetProductByID((int)cartItem.product_id);

            // Remove from cart. Note that for simplicity, we're 
            // removing all rather than decrementing the count.
            for (int i = 0; i < cartItem.count; i++)
            {
                cart.RemoveFromCart(int.Parse(id));
            }

            double CartTotal = cart.GetTotalValueCartItem();
            int CartCount = cart.GetCountCartItem();
            var list = cart.GetListCartItemByCartID();
            var list_cart_view = new System.Collections.Generic.List<CartViewModels>();
            foreach (var ite in list)
            {
                var _product = web.core.Models.productModels.GetProductViewByID(ite.product_id ?? 0, Request.Url.PathAndQuery);
                var cart_view = new web.core.ViewModels.CartViewModels();
                cart_view.record_id = ite.record_id;
                cart_view.product_id = ite.product_id;
                cart_view.product_name = _product.product_name;
                cart_view.product_alias = _product.product_alias;
                cart_view.price = _product.price;
                cart_view.quantity = _product.quantity;
                cart_view.images = _product.images;
                cart_view.count = ite.count;
                list_cart_view.Add(cart_view);
            }

            var results = web.bussines.my_extensions.ToJSON(list_cart_view) + "|" + CartTotal + "|" + CartCount;
            return Json(results);
        }

        public ActionResult AddToCartAjaxTransfer(string id)
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);
            //remove
            cart.EmptyCart();
            //add new
            var product = productModels.GetProductByID(int.Parse(id));
            cart.AddToCart(product);
            var result = "1";
            // Go back to the main store page for more shopping
            return Json(result);
        }

        [HttpPost]
        public ActionResult UpdateToCart(string id)
        {
            var arr = id.Split('|');
            var re_arr = arr[0].Split(',');
            var id_arr = arr[1].Split(',');
            var qty_arr = arr[2].Split(',');

            // get Product
            var product = productModels.GetProductByID(int.Parse(id_arr[0]));
            // Add it to the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext);

            var list = cart.GetListCartItemByCartID();
            for(int i=0;i<list.Count;i++)
            {
                if (list[i].product_id == int.Parse(id_arr[i])) {
                    if (int.Parse(qty_arr[i]) != list[i].count) {
                        cart.UpdateProductInCart(int.Parse(re_arr[i]),list[i].product_id, int.Parse(qty_arr[i]));
                    }
                }
            }

            double CartTotal = cart.GetTotalValueCartItem();
            int CartCount = cart.GetCountCartItem();
            var list_cart_view = new System.Collections.Generic.List<CartViewModels>();
            foreach (var ite in list)
            {
                var _product = web.core.Models.productModels.GetProductViewByID(ite.product_id ?? 0, Request.Url.PathAndQuery);
                var cart_view = new web.core.ViewModels.CartViewModels();
                cart_view.record_id = ite.record_id;
                cart_view.product_id = ite.product_id;
                cart_view.product_name = _product.product_name;
                cart_view.product_alias = _product.product_alias;
                cart_view.price = _product.price;
                cart_view.quantity = _product.quantity;
                cart_view.images = _product.images;
                cart_view.count = ite.count;
                list_cart_view.Add(cart_view);
            }

            var results = web.bussines.my_extensions.ToJSON(list_cart_view) + "|" + CartTotal + "|" + CartCount;
            return Json(results);

            //// Go back to the main store page for more shopping
            //string result = arr.ToString();
            //return Json(result);
        }

        [HttpPost]
        public ActionResult RemoveFromCart(int id)
        {
            // Remove the item from the cart
            var cart = ShoppingCart.GetCart(this.HttpContext);

            var cartItem = ShoppingCart.GetCartbyRecordID(id);
            // Get product
            var product = productModels.GetProductByID((int)cartItem.product_id);

            // Remove from cart. Note that for simplicity, we're 
            // removing all rather than decrementing the count.
            for (int i = 0; i < cartItem.count; i++)
            {
                cart.RemoveFromCart(id);
            }

            // Display the confirmation message
            //var results = new ShoppingCartRemoveViewModels
            //{
            //    Message = Server.HtmlEncode(product.ProductName) +
            //        " đã được xóa khỏi giỏ hàng của bạn.",
            //    CartTotal = cart.GetTotalValueCartItem(languageId),
            //    CartCount = cart.GetCountCartItem(),
            //    DeleteId = id
            //};
            double CartTotal = cart.GetTotalValueCartItem();
            int CartCount = cart.GetCountCartItem();

            var results = id + "|" + CartTotal + "|" + CartCount;

            return Json(results);
        }
    }
}